<!DOCTYPE html>
<html lang="en">
    <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta http-equiv="content-type" content="text/html; charset=utf-8">

      <!-- Enable responsiveness on mobile devices-->
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">

      <title>zmant</title>

      
        <link rel="alternate" type="application/rss+xml" title="RSS" href="https:&#x2f;&#x2f;itanq.github.io&#x2f;rss.xml">
      

      
          <link rel="stylesheet" href="https:&#x2f;&#x2f;itanq.github.io&#x2f;site.css">
      

      
      
    </head>

    <body>
        <div class="container">

            <div id="mobile-navbar" class="mobile-navbar">
              <div class="mobile-header-logo">
                <a href="/" class="logo">zmant</a>
              </div>
            </div>

            <header id="header">
                <div class="logo"><a href="https:&#x2f;&#x2f;itanq.github.io">zmant</a></div>
                <nav class="menu">
                    <ul>
                        
                            <li>
                                <a href="https://itanq.github.io">
                                    Home
                                </a>
                            </li>
                        
                            <li>
                                <a href="https://itanq.github.io&#x2f;categories">
                                    Categories
                                </a>
                            </li>
                        
                            <li>
                                <a href="https://itanq.github.io&#x2f;tags">
                                    Tags
                                </a>
                            </li>
                        
                            <li>
                                <a href="https://itanq.github.io&#x2f;about">
                                    About
                                </a>
                            </li>
                        
                    </ul>
                </nav>
            </header>

            <main>
                <div class="content" id="mobile-panel">
                    



<article class="post">
    
    <header class="post__header">
        <h1 class="post__title">
            <a href="https:&#x2f;&#x2f;itanq.github.io&#x2f;xuan-ran-jing-ling&#x2f;">渲染精灵</a>
        </h1>
        <div class="post__meta">
            <span class="post__time">2016-03-09</span>
            <span class="post__word_count">2463 Words</span>
            
        </div>
    </header>

    <div class="post-content">
      <h2 id="xuan-ran-jing-ling">渲染精灵</h2>
<p>为了给我们当前这个黑漆漆的游戏世界带来一点生机，我们将会渲染一些精灵(sprite)来填补一些空虚。精灵有很多种定义，但主要是指一个2D图片，它通常是和一些属性数据一起使用，比如用一些数据来表示它在世界坐标下的位置，一个旋转的角度以及一个表示二维空间的大小的变量。在2D游戏中，我们主要使用精灵来渲染图片/纹理对象。</p>
<p id="zola-continue-reading"><a name="continue-reading"></a></p>
<p>就像前面那些教程里做的那样，我们可以把顶点数据传到GPU并且手动的通过一些操作来创建一些2D形状。然而，在一个大型应用中，就像我们正在做的这个，我们还是宁愿在渲染2D形状上做一些抽象。如果我们手动的去定义并转换每一个对象，那就相当的凌乱了。</p>
<p>在这里，我们将会定义一个用最少的代码去渲染大量的精灵的渲染类。这样，我们就可以从像散沙一样的OpenGL渲染代码中摘要出游戏代码，这是在一个大型应用中常用的做法。尽管我们首先要做的是设置一个合适的投影矩阵。</p>
<h2 id="2dtou-ying-ju-zhen">2D投影矩阵</h2>
<p>从这个<a href="https://learnopengl-cn.github.io/01%20Getting%20started/08%20Coordinate%20Systems/">坐标系统</a>教程我们明白了投影矩阵的作用是把视图空间坐标转化为标准化设备坐标。通过生成合适的投影矩阵，我们就可以在不同的坐标系下计算，这可能比把所有的坐标指定为标准化设备坐标（再计算）要更容易处理。</p>
<p>我们并不需要在坐标系执行任何的透视应用，因为这个游戏完全是在二维平面，所以一个正交投影矩阵就可以很好的工作了。因为正交投影矩阵几乎是直接把整个坐标变换到裁切空间，我们可以定义如下的矩阵来把世界坐标指定为屏幕坐标：</p>
<pre style="background-color:#2b303b;">
<span style="color:#c0c5ce;">glm::mat4 projection = </span><span style="color:#bf616a;">glm::ortho</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">,</span><span style="color:#d08770;">800.0f</span><span style="color:#c0c5ce;">,</span><span style="color:#d08770;">600.0f</span><span style="color:#c0c5ce;">,</span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">,-</span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">,</span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">);
</span></pre>
<p>前面的四个参数依次指定了投影椎体左、右、下、上边界。这个投影矩阵把所有在<code>0</code>到<code>800</code>之间的<code>x</code>坐标变换到<code>-1</code>到<code>1</code>之间以及把所有在<code>0</code>到<code>600</code>之间的<code>y</code>坐标变换到<code>-1</code>到<code>1</code>之间。这里我们指定了视椎体上部的<code>y</code>坐标值为<code>0</code>，同时其下部的<code>y</code>坐标值为<code>600</code>。结果就是这个场景的左上角坐标为<code>(0,0)</code>，右下角坐标为<code>(800,600)</code>，就像屏幕坐标那样。视图空间坐标直接对应像素坐标。</p>
<img alt="projection" src="../../img/06/03/projection.png"/>
<p>这允许我们指定每个坐标都等于它们在屏幕上最终的像素坐标，这在2D游戏里相当直观。</p>
<h2 id="xuan-ran-jing-ling-1">渲染精灵</h2>
<p>渲染一个实际的精灵不因太复杂化。我们创建一个纹理四边形，并且在使用了前面预先定义的正交投影矩阵（变换到标准化设备坐标）之后我们可以使用模型矩阵来变换它。</p>
<p>!!! important
由于<code>Breakout</code>游戏是一个静态游戏，这里不需要视图/摄像机矩阵，我们可以直接使用投影矩阵把世界空间坐标变换到裁切空间坐标</p>
<p>为了变换精灵我们使用了下面的顶点着色器：</p>
<pre style="background-color:#2b303b;">
<span style="color:#c0c5ce;">#version </span><span style="color:#d08770;">330</span><span style="color:#c0c5ce;"> core
</span><span style="color:#bf616a;">layout </span><span style="color:#c0c5ce;">(location = </span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">) in vec4 vertex; </span><span style="color:#65737e;">// &lt;vec2 position, vec2 texCoords&gt;
</span><span style="color:#c0c5ce;">
out vec2 TexCoords;

uniform mat4 model;
uniform mat4 projection;

</span><span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">()
{
    TexCoords = vertex.</span><span style="color:#bf616a;">zw</span><span style="color:#c0c5ce;">;
    gl_Position = projection * model * </span><span style="color:#bf616a;">vec4</span><span style="color:#c0c5ce;">(vertex.</span><span style="color:#bf616a;">xy</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0</span><span style="color:#c0c5ce;">);
}
</span></pre>
<p>注意，我们使用了一个单一的<code>vec4</code>变量来存储了位置和纹理坐标数据。因为位置和纹理坐标数据都只包含了两个浮点型数据，所以我们可以把他们组合在一起作为一个单一的顶点属性。</p>
<p>像素着色器相对比较简单。我们设置了一个纹理和颜色向量，她们两个都会对像素最后的颜色产生影响。同样也设置了一个<code>uniform</code>颜色向量，我们就可以很容易的在游戏代码里面改变精灵的颜色。</p>
<pre style="background-color:#2b303b;">
<span style="color:#c0c5ce;">#version </span><span style="color:#d08770;">330</span><span style="color:#c0c5ce;"> core
in vec2 TexCoords;
out vec4 color;

uniform sampler2D image;
uniform vec3 spriteColor;

</span><span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">main</span><span style="color:#c0c5ce;">()
{
    color = </span><span style="color:#bf616a;">vec4</span><span style="color:#c0c5ce;">(spriteColor, </span><span style="color:#d08770;">1.0</span><span style="color:#c0c5ce;">) * </span><span style="color:#bf616a;">texture</span><span style="color:#c0c5ce;">(image, TexCoords);
}
</span></pre>
<p>为了让渲染精灵的代码更加有条理，我们定义了一个<code>SpriteRenderer</code>类，只需要一个单一的函数就可以渲染精灵，它的定义如下：</p>
<pre style="background-color:#2b303b;">
<span style="color:#b48ead;">class </span><span style="color:#ebcb8b;">SpriteRenderer
</span><span style="color:#eff1f5;">{
    </span><span style="color:#b48ead;">public</span><span style="color:#eff1f5;">:
        </span><span style="color:#8fa1b3;">SpriteRenderer</span><span style="color:#eff1f5;">(Shader </span><span style="color:#c0c5ce;">&amp;</span><span style="color:#bf616a;">shader</span><span style="color:#eff1f5;">);
        </span><span style="color:#8fa1b3;">~SpriteRenderer</span><span style="color:#eff1f5;">();

        </span><span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">DrawSprite</span><span style="color:#eff1f5;">(Texture2D </span><span style="color:#c0c5ce;">&amp;</span><span style="color:#bf616a;">texture</span><span style="color:#eff1f5;">, glm::vec2 </span><span style="color:#bf616a;">position</span><span style="color:#eff1f5;">,
            glm::vec2 </span><span style="color:#bf616a;">size </span><span style="color:#c0c5ce;">= </span><span style="color:#bf616a;">glm::vec2</span><span style="color:#eff1f5;">(</span><span style="color:#d08770;">10</span><span style="color:#eff1f5;">, </span><span style="color:#d08770;">10</span><span style="color:#eff1f5;">), GLfloat </span><span style="color:#bf616a;">rotate </span><span style="color:#c0c5ce;">= </span><span style="color:#d08770;">0.0f</span><span style="color:#eff1f5;">,
            glm::vec3 </span><span style="color:#bf616a;">color </span><span style="color:#c0c5ce;">= </span><span style="color:#bf616a;">glm::vec3</span><span style="color:#eff1f5;">(</span><span style="color:#d08770;">1.0f</span><span style="color:#eff1f5;">));
    </span><span style="color:#b48ead;">private</span><span style="color:#eff1f5;">:
        Shader shader;
        GLuint quadVAO;

        </span><span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">initRenderData</span><span style="color:#eff1f5;">();
}</span><span style="color:#c0c5ce;">;
</span></pre>
<p><code>SpriteRenderer</code>类封装了一个着色器对象，一个顶点数组对象以及一个渲染和初始化函数。它的构造函数接受一个着色器对象用于后面的渲染。</p>
<h3 id="chu-shi-hua">初始化</h3>
<p>首先，让我们探究一下负责配置<code>quadVAO</code>的<code>initRenderData</code>h函数：</p>
<pre style="background-color:#2b303b;">
<span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">SpriteRenderer::initRenderData</span><span style="color:#c0c5ce;">()
{
    </span><span style="color:#65737e;">// Configure VAO/VBO
</span><span style="color:#c0c5ce;">    GLuint VBO;
    GLfloat vertices[] = {
        </span><span style="color:#65737e;">// Pos      // Tex
        </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">,
        </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">,
        </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">,

        </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">,
        </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">,
        </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f
    </span><span style="color:#c0c5ce;">};

    </span><span style="color:#bf616a;">glGenVertexArrays</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">1</span><span style="color:#c0c5ce;">, &amp;</span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">quadVAO</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">glGenBuffers</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">1</span><span style="color:#c0c5ce;">, &amp;VBO);

    </span><span style="color:#bf616a;">glBindBuffer</span><span style="color:#c0c5ce;">(GL_ARRAY_BUFFER, VBO);
    </span><span style="color:#bf616a;">glBufferData</span><span style="color:#c0c5ce;">(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    </span><span style="color:#bf616a;">glBindVertexArray</span><span style="color:#c0c5ce;">(</span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">quadVAO</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">glEnableVertexAttribArray</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">glVertexAttribPointer</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">4</span><span style="color:#c0c5ce;">, GL_FLOAT, GL_FALSE, </span><span style="color:#d08770;">4 </span><span style="color:#c0c5ce;">* sizeof(GLfloat), (GLvoid*)</span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">glBindBuffer</span><span style="color:#c0c5ce;">(GL_ARRAY_BUFFER, </span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">glBindVertexArray</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">);
}
</span></pre>
<p>在这我们首先定义了一组以四边形的左上角为<code>(0,0)</code>的顶点坐标。这意味着当我们在四边形上申请一个移动或伸缩变换的时候，四边形的左上角会被变换。这在2D图形或GUI这类元素的位置是相对于元素左上角的系统而言很常见。</p>
<p>下面我们简单的向<code>GPU</code>传递了顶点数据并且配置了顶点属性，这里的情况是只有一个单一的顶点属性。我们仅仅需要为每一个精灵渲染器定义一个单一的<code>VAO</code>，因为他们的顶点数据都是一样的。</p>
<h3 id="xuan-ran">渲染</h3>
<p>渲染精灵并不是太难；我们使用精灵渲染器的着色器对象，配置一个模型矩阵并且设置相关的<code>uniform</code>变量。这里最重要的就是变换的顺序：</p>
<pre style="background-color:#2b303b;">
<span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">SpriteRenderer::DrawSprite</span><span style="color:#c0c5ce;">(Texture2D &amp;</span><span style="color:#bf616a;">texture</span><span style="color:#c0c5ce;">, glm::vec2 </span><span style="color:#bf616a;">position</span><span style="color:#c0c5ce;">,
  glm::vec2 </span><span style="color:#bf616a;">size</span><span style="color:#c0c5ce;">, GLfloat </span><span style="color:#bf616a;">rotate</span><span style="color:#c0c5ce;">, glm::vec3 </span><span style="color:#bf616a;">color</span><span style="color:#c0c5ce;">)
{
    </span><span style="color:#65737e;">// Prepare transformations
    </span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">shader</span><span style="color:#c0c5ce;">.</span><span style="color:#bf616a;">Use</span><span style="color:#c0c5ce;">();
    glm::mat4 model;
    model = </span><span style="color:#bf616a;">glm::translate</span><span style="color:#c0c5ce;">(model, </span><span style="color:#bf616a;">glm::vec3</span><span style="color:#c0c5ce;">(position, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">));

    model = </span><span style="color:#bf616a;">glm::translate</span><span style="color:#c0c5ce;">(model, </span><span style="color:#bf616a;">glm::vec3</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0.5f </span><span style="color:#c0c5ce;">* size.</span><span style="color:#bf616a;">x</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.5f </span><span style="color:#c0c5ce;">* size.</span><span style="color:#bf616a;">y</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">));
    model = </span><span style="color:#bf616a;">glm::rotate</span><span style="color:#c0c5ce;">(model, rotate, </span><span style="color:#bf616a;">glm::vec3</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">));
    model = </span><span style="color:#bf616a;">glm::translate</span><span style="color:#c0c5ce;">(model, </span><span style="color:#bf616a;">glm::vec3</span><span style="color:#c0c5ce;">(-</span><span style="color:#d08770;">0.5f </span><span style="color:#c0c5ce;">* size.</span><span style="color:#bf616a;">x</span><span style="color:#c0c5ce;">, -</span><span style="color:#d08770;">0.5f </span><span style="color:#c0c5ce;">* size.</span><span style="color:#bf616a;">y</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">));

    model = </span><span style="color:#bf616a;">glm::scale</span><span style="color:#c0c5ce;">(model, </span><span style="color:#bf616a;">glm::vec3</span><span style="color:#c0c5ce;">(size, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">));

    </span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">shader</span><span style="color:#c0c5ce;">.</span><span style="color:#bf616a;">SetMatrix4</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">model</span><span style="color:#c0c5ce;">&quot;, model);
    </span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">shader</span><span style="color:#c0c5ce;">.</span><span style="color:#bf616a;">SetVector3f</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">spriteColor</span><span style="color:#c0c5ce;">&quot;, color);

    </span><span style="color:#bf616a;">glActiveTexture</span><span style="color:#c0c5ce;">(GL_TEXTURE0);
    texture.</span><span style="color:#bf616a;">Bind</span><span style="color:#c0c5ce;">();

    </span><span style="color:#bf616a;">glBindVertexArray</span><span style="color:#c0c5ce;">(</span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">quadVAO</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">glDrawArrays</span><span style="color:#c0c5ce;">(GL_TRIANGLES, </span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">6</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">glBindVertexArray</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">);
}
</span></pre>
<p>当试图在一个场景中用旋转矩阵和伸缩矩阵放置一个对象的时候，建议首先做伸缩变换，然后旋转最后是移动变换。因为矩阵乘法是从右向左执行的，所以我们变换的矩阵顺序是相反的：移动，旋转，缩放。</p>
<p>旋转变换可能看起来稍微有点让人望而却步。我们从<a href="https://learnopengl-cn.github.io/01%20Getting%20started/07%20Transformations/">变换</a>教程里面知道旋转总是围绕原点<code>(0,0)</code>转动的。因为我们指定了四边形的左上角为<code>(0,0)</code>，所有的旋转都是围绕<code>(0,0)</code>的。主要是这个旋转的原点是四边形的左上角，这样就会造成不太友好的旋转效果。我们想要做的就是把旋转的点移到四边形的中心，这样旋转就是围绕四边形中心而不是左上角了。我们通过在旋转之前把旋转点移动到四边形中心解决了这个问题。</p>
<img alt="rotate" src="../../img/06/03/rotation-origin.png"/>
<p>因为我们首先缩放了这个四边形，我们不得不在把原点变换到精灵中心的时候输入一个<code>size</code>变量(这就是我们乘了精灵的<code>size</code>向量)。一旦应用了旋转变换之后，我们就需要做上一次变换的逆变换。</p>
<p>把所有变换组合起来我们就可以以我们喜欢的方式移动、缩放并且旋转每一个精灵了。在下面你可以找到完整的精灵渲染器的源代码：</p>
<blockquote>
<ul>
<li>SpriteRenderer: <a href="http://learnopengl.com/code_viewer.php?code=in-practice/breakout/sprite_renderer.h">header</a>,<a href="http://learnopengl.com/code_viewer.php?code=in-practice/breakout/sprite_renderer">code</a></li>
</ul>
</blockquote>
<h2 id="ni-hao-jing-ling">你好，精灵</h2>
<p>使用<code>SpriteRenderer</code>类我们有了把实际图片渲染到屏幕的能力！让我们来在游戏代码里面初始化一个精灵并且加载一个我们最喜爱的<a href="http://learnopengl.com/img/textures/awesomeface.png">纹理</a>：</p>
<pre style="background-color:#2b303b;">
<span style="color:#c0c5ce;">SpriteRenderer  *Renderer;

</span><span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">Game::Init</span><span style="color:#c0c5ce;">()
{
    </span><span style="color:#65737e;">// Load shaders
    </span><span style="color:#bf616a;">ResourceManager::LoadShader</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">shaders/sprite.vs</span><span style="color:#c0c5ce;">&quot;, &quot;</span><span style="color:#a3be8c;">shaders/sprite.frag</span><span style="color:#c0c5ce;">&quot;, </span><span style="color:#d08770;">nullptr</span><span style="color:#c0c5ce;">, &quot;</span><span style="color:#a3be8c;">sprite</span><span style="color:#c0c5ce;">&quot;);
    </span><span style="color:#65737e;">// Configure shaders
</span><span style="color:#c0c5ce;">    glm::mat4 projection = </span><span style="color:#bf616a;">glm::ortho</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, static_cast&lt;GLfloat&gt;(</span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">Width</span><span style="color:#c0c5ce;">),
        static_cast&lt;GLfloat&gt;(</span><span style="color:#bf616a;">this</span><span style="color:#c0c5ce;">-&gt;</span><span style="color:#bf616a;">Height</span><span style="color:#c0c5ce;">), </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, -</span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">ResourceManager::GetShader</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">sprite</span><span style="color:#c0c5ce;">&quot;).</span><span style="color:#bf616a;">Use</span><span style="color:#c0c5ce;">().</span><span style="color:#bf616a;">SetInteger</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">image</span><span style="color:#c0c5ce;">&quot;, </span><span style="color:#d08770;">0</span><span style="color:#c0c5ce;">);
    </span><span style="color:#bf616a;">ResourceManager::GetShader</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">sprite</span><span style="color:#c0c5ce;">&quot;).</span><span style="color:#bf616a;">SetMatrix4</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">projection</span><span style="color:#c0c5ce;">&quot;, projection);
    </span><span style="color:#65737e;">// Set render-specific controls
</span><span style="color:#c0c5ce;">    Renderer = </span><span style="color:#b48ead;">new </span><span style="color:#bf616a;">SpriteRenderer</span><span style="color:#c0c5ce;">(</span><span style="color:#bf616a;">ResourceManager::GetShader</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">sprite</span><span style="color:#c0c5ce;">&quot;));
    </span><span style="color:#65737e;">// Load textures
    </span><span style="color:#bf616a;">ResourceManager::LoadTexture</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">textures/awesomeface.png</span><span style="color:#c0c5ce;">&quot;, GL_TRUE, &quot;</span><span style="color:#a3be8c;">face</span><span style="color:#c0c5ce;">&quot;);
}
</span></pre>
<p>然后在渲染函数里面我们渲染一下我们的吉祥物来看看是否一切都按正确的方式工作了：</p>
<pre style="background-color:#2b303b;">
<span style="color:#b48ead;">void </span><span style="color:#8fa1b3;">Game::Render</span><span style="color:#c0c5ce;">()
{
    Renderer-&gt;</span><span style="color:#bf616a;">DrawSprite</span><span style="color:#c0c5ce;">(</span><span style="color:#bf616a;">ResourceManager::GetTexture</span><span style="color:#c0c5ce;">(&quot;</span><span style="color:#a3be8c;">face</span><span style="color:#c0c5ce;">&quot;),
        </span><span style="color:#bf616a;">glm::vec2</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">200</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">200</span><span style="color:#c0c5ce;">), </span><span style="color:#bf616a;">glm::vec2</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">300</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">400</span><span style="color:#c0c5ce;">), </span><span style="color:#d08770;">45.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#bf616a;">glm::vec3</span><span style="color:#c0c5ce;">(</span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">1.0f</span><span style="color:#c0c5ce;">, </span><span style="color:#d08770;">0.0f</span><span style="color:#c0c5ce;">));
}
</span></pre>
<p>这里我们把精灵放置在靠近屏幕中心的位置，它的高度稍微有点大于宽度。我们同样也把它旋转了<code>45.0f</code>度并且给了一个绿色。注意，我们设定的精灵的位置是精灵四边形左上角的位置。</p>
<p>如果你一切都做对了你应该可以看到下面的输出：</p>
<img alt="output" src="../../img/06/03/rendering-sprites.png"/>
<p>你可以在<a href="http://learnopengl.com/code_viewer.php?code=in-practice/breakout/game_rendering-sprites">这里</a>找到更新后的游戏源代码。</p>
<p>目前我们的渲染系统正确工作了，我们在下一个教程里设置了游戏的等级，它在那里将会有更好的用处。</p>

    </div>

    
    

    <div class="post-footer">
        
            
                <div class="post-tags">
                    
                        <a href="https:&#x2f;&#x2f;itanq.github.io&#x2f;tags&#x2f;opengl&#x2f;">#opengl</a>
                    
                </div>
            
            
                <div class="post-nav">
                    
                        <a class="previous" href="https:&#x2f;&#x2f;itanq.github.io&#x2f;zhun-bei-gong-zuo&#x2f;">‹ 准备工作</a>
                    
                    
                        <a class="next" href="https:&#x2f;&#x2f;itanq.github.io&#x2f;li-zi-xi-tong&#x2f;">粒子系统 ›</a>
                    
                    
                    
                </div>
            

        

    </div>

    
    
</article>


                </div>
            </main>

            
            
        </div>

    </body>

</html>
